// pages/zxm-calendar/index.js
const moment = require('moment')
let months = []
for (let i = 0; i < 12; i++) {
  months.push((i + 1) + '月')
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 近七天时间
    sevenDays: [],
    // 近4周
    prevWeek:[],
    currentWeek: 0,
    // 1-12月
    months
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(this.getLastNDays());
    const sevenDays = this.getLastDate()
    const prevWeek = this.getPrevIWeek(4)
    const nextWeek = this.getNextIWeek(4)
    console.log(sevenDays, prevWeek, nextWeek);
    this.setData({
      sevenDays,
      prevWeek,
      currentWeek: prevWeek.length - 1
    })
  },
  prev(){
    if (this.data.currentWeek > 0 &&　this.data.currentWeek <= this.data.prevWeek.length - 1) {
      this.setData({
        currentWeek: this.data.currentWeek - 1
      })
    }
  },
  next(){
    if (this.data.currentWeek >= 0 &&　this.data.currentWeek < this.data.prevWeek.length - 1) {
      this.setData({
        currentWeek: this.data.currentWeek + 1
      })
    }
  },
  /**
   * @params
   * 获取前 i 周的周一和周日日期
   * 当 i=1，获取的是上周一和上周日的日期；
   * 当 i=2，获取的是上上周一和上上周日的日期
   * ...以此类推
   */
  getPrevIWeek(weeks){
    let week = weeks // 周数
    let weekOfDay = new Date().getDay() // 今天星期几
    let result = []
    for (let i = 0; i < week; i++) {
      let last_monday = '', last_sunday = '', obj = {};
      last_monday = moment().subtract(weekOfDay + 7 * i - 1, 'days').format('MM月DD日');
      last_sunday = moment().subtract(weekOfDay + 7 * (i - 1), 'days').format('MM月DD日');
      obj.date = `${last_monday} - ${last_sunday}`
      result.unshift(obj)
    }
    return result
  },
  /**
   * @params
   * 获取后 i 周的周一和周日日期
   * 当 i = 1，获取的是下周一和下周日的日期
   * 当 i = 2，获取的是下下周一和下下周日的日期
   * ...以此类推
   */
  getNextIWeek(weeks){
    let week = weeks // 周数
    let weekOfDay = new Date().getDay(); // 今天星期几
    let result = []
    for (let i = 0; i < week; i++) {
      let last_monday = '', last_sunday = '', obj = {};
      last_monday = moment().add((7 - weekOfDay) + 7 * (i - 1) + 1, 'd').format('M月DD日');
      last_sunday = moment().add((7 - weekOfDay) + 7 * i, 'd').format('M月DD日');
      obj.date = `${last_monday} - ${last_sunday}`
      result.push(obj)
    }
    return result
  },

  getLastNDays(N = 7){
    const daysOfWeek = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
    let result = []
    for (let i = 0; i < N; i++) {
      const lastIDate = moment().add(1, 'days').subtract(i + 1, 'days')
      const lastIDateFormat = lastIDate.format('YYYY-MM-DD')
      const year = moment(lastIDate).format('YYYY')
      const month = moment(lastIDate).format('MM')
      const day = moment(lastIDate).format('DD')
      const week =  moment(lastIDate.valueOf()).day()
      result.push({date: lastIDateFormat, year, month, day, week: daysOfWeek[week]})
    }
    return result
  },


  getLastDate(value){
    // 获取当前日期
    const daysOfWeek = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
    const today = value ? new Date(value) : new Date()
    let dateList = []
    // 循环获取过去7天的日期
    for (let i = 6; i >= 0; i--) {
      const currentDay = new Date(today) //复制今天的日期对象
      currentDay.setDate(currentDay.getDate() - i) //设置为前i天的日期，如果获取后i天的日期改成currentDay
      var year = String(currentDay.getFullYear()) //年份
      var month = String(currentDay.getMonth() + 1).padStart(2, '0')
      var day = String(currentDay.getDate()).padStart(2, '0')
      var week = daysOfWeek[currentDay.getDay()]
      var formatterDate = `${year}-${month}-${day}`
      dateList.unshift({date: formatterDate, year, month, day, week})
    }
    return dateList
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})